package com.jhgsys.internal.incentive.service.impl;

import com.jhgsys.internal.incentive.entity.IncentiveRewardFile;
import com.jhgsys.internal.incentive.mapper.IncentiveRewardFileMapper;
import com.jhgsys.internal.incentive.service.IIncentiveRewardFileService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 安全奖励附件 Service实现
 *
 * @author wanghjw
 * @date 2024-09-03 14:08:33
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class IncentiveRewardFileServiceImpl extends ServiceImpl<IncentiveRewardFileMapper, IncentiveRewardFile> implements IIncentiveRewardFileService {

    /**
     * 查询（分页）
     * @author wanghjw
     * @date 2024-09-03 14:08:33
     * @param request QueryRequest
     * @param incentiveRewardFile incentiveRewardFile
     * @return IPage<IncentiveRewardFile>
    */
    @Override
    public IPage<IncentiveRewardFile> findIncentiveRewardFilePage(QueryRequest request, IncentiveRewardFile incentiveRewardFile) {
        LambdaQueryWrapper<IncentiveRewardFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<IncentiveRewardFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author wanghjw
     * @date 2024-09-03 14:08:33
     * @param incentiveRewardFile incentiveRewardFile
     * @return List<IncentiveRewardFile>
     */
    @Override
    public List<IncentiveRewardFile> findIncentiveRewardFileList(IncentiveRewardFile incentiveRewardFile) {
	    LambdaQueryWrapper<IncentiveRewardFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IncentiveRewardFile::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(incentiveRewardFile.getRewardApplyId())){
            queryWrapper.eq(IncentiveRewardFile::getRewardApplyId,incentiveRewardFile.getRewardApplyId());
        }
        if(StringUtils.isNotBlank(incentiveRewardFile.getRewardFileType())){
            queryWrapper.eq(IncentiveRewardFile::getRewardFileType,incentiveRewardFile.getRewardFileType());
        }
        if(StringUtils.isNotBlank(incentiveRewardFile.getTenantId())){
            queryWrapper.eq(IncentiveRewardFile::getTenantId,incentiveRewardFile.getTenantId());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghjw
    * @date 2024-09-03 14:08:33
    * @param id
    * @return IncentiveRewardFile
    */
    @Override
    public IncentiveRewardFile findIncentiveRewardFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author wanghjw
     * @date 2024-09-03 14:08:33
     * @param incentiveRewardFile incentiveRewardFile
     */
    @Override
    @Transactional
    public void createIncentiveRewardFile(IncentiveRewardFile incentiveRewardFile) {
        incentiveRewardFile.setCreatedTime(new Date());
        this.save(incentiveRewardFile);
    }

    /**
     * 修改
     * @author wanghjw
     * @date 2024-09-03 14:08:33
     * @param incentiveRewardFile incentiveRewardFile
     */
    @Override
    @Transactional
    public void updateIncentiveRewardFile(IncentiveRewardFile incentiveRewardFile) {
        incentiveRewardFile.setUpdatedTime(new Date());
        this.saveOrUpdate(incentiveRewardFile);
    }

    /**
     * 逻辑删除
     * @author wanghjw
     * @date 2024-09-03 14:08:33
     * @param incentiveRewardFile incentiveRewardFile
     * @param incentiveRewardFileIds incentiveRewardFileIds
     */
    @Override
    @Transactional
    public void deleteIncentiveRewardFile(IncentiveRewardFile incentiveRewardFile,String incentiveRewardFileIds) {
        List<String> list = Arrays.asList(incentiveRewardFileIds.split(StringPool.COMMA));
        this.baseMapper.update(incentiveRewardFile,new LambdaQueryWrapper<IncentiveRewardFile>().in(IncentiveRewardFile::getRewardApplyFileId,list));
	}

    /**
    * 物理删除
    * @author wanghjw
    * @date 2024-09-03 14:08:33
    * @param incentiveRewardFileIds incentiveRewardFileIds
    */
    @Override
    @Transactional
    public void physicallyDeleteIncentiveRewardFile(String incentiveRewardFileIds) {
        List<String> list = Arrays.asList(incentiveRewardFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<IncentiveRewardFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(IncentiveRewardFile::getRewardApplyFileId,list);
        this.remove(wapper);
    }
}
